package com.ksd.pug.service.hotelcomment;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ksd.orm.pug.mapper.HotelCommentMapper;
import com.ksd.pug.commons.enums.ResultStatusEnumA;
import com.ksd.pug.commons.ex.PugValidationException;
import com.ksd.pug.commons.utils.fn.asserts.Vsserts;
import com.ksd.pug.pojo.HotelComment;
import com.ksd.pug.vo.HotelCommentVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;


/**
 * HotelCommentServiceImpl实现类
 * 创建人:yykk<br/>
 * 时间：2022-04-18 01:07:39 <br/>
 * 源码下载：前台代码 git clone https://gitee.com/kekesam/kuangstudy-pug-ui.git
 * 飞哥B站地址：后台代码 git clone https://gitee.com/kekesam/kuangstudy-pug-parent.git
 *
 * @version 1.0.0<br />
 */
@Service
@Slf4j
public class HotelCommentServiceImpl extends ServiceImpl<HotelCommentMapper, HotelComment> implements IHotelCommentService {

    /**
     * 查询分页&搜索酒店评论
     *
     * @param hotelcommentVo
     * @return IPage<HotelComment>
     * 创建人:yykk
     * 创建时间：2022-04-18 01:07:39
     * @version 1.0.0
     */
    @Override
    public IPage<HotelComment> findHotelCommentPage(HotelCommentVo hotelcommentVo) {
        // 设置分页信息
        Page<HotelComment> page = new Page<>(hotelcommentVo.getPageNo(), hotelcommentVo.getPageSize());
        // 设置查询条件
        LambdaQueryWrapper<HotelComment> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        // lambdaQueryWrapper.select(HotelComment.class, column -> !column.getColumn().equals("description"));
        // 根据关键词搜索信息
        lambdaQueryWrapper.like(Vsserts.isNotEmpty(hotelcommentVo.getKeyword()), HotelComment::getUsernickname, hotelcommentVo.getKeyword());
        // 根据关键词搜索信息
        lambdaQueryWrapper.like(Vsserts.isNotEmpty(hotelcommentVo.getKeyword()), HotelComment::getReplyUsernickname, hotelcommentVo.getKeyword());
        // 多列搜索
        // lambdaQueryWrapper.and(Vsserts.isNotEmpty(hotelcommentVo.getKeyword()),wrapper -> wrapper
        //         .like(HotelComment::getName, hotelcommentVo.getKeyword())
        //         .or()
        //         .like(HotelComment::getCategoryName, hotelcommentVo.getKeyword())
        // );
        // 根据时间排降序
        lambdaQueryWrapper.orderByDesc(HotelComment::getCreateTime);
        // 查询分页返回
        IPage<HotelComment> results = this.page(page, lambdaQueryWrapper);
        return results;
    }

    /**
     * 查询酒店评论列表信息
     *
     * @return
     * @method: findHotelCommentList
     * @result : List<HotelComment>
     * 创建人:yykk
     * 创建时间：2022-04-18 01:07:39
     * @version 1.0.0
     */
    @Override
    public List<HotelComment> findHotelCommentList() {
        // 1：设置查询条件
        LambdaQueryWrapper<HotelComment> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        // 3: 查询返回
        return this.list(lambdaQueryWrapper);
    }

    /**
     * 根据id查询酒店评论明细信息
     *
     * @param id
     * @return HotelComment
     * 创建人:yykk
     * 创建时间：2022-04-18 01:07:39
     * @version 1.0.0
     */
    @Override
    public HotelComment getHotelCommentById(Long id) {
        return this.getById(id);
    }


    /**
     * 保存&修改酒店评论
     *
     * @param hotelcomment
     * @return HotelComment
     * 创建人:yykk
     * 创建时间：2022-04-18 01:07:39
     * @version 1.0.0
     */
    @Override
    public HotelComment saveupdateHotelComment(HotelComment hotelcomment) {
        boolean flag = this.saveOrUpdate(hotelcomment);
        return flag ? hotelcomment : null;
    }


    /**
     * 根据id删除酒店评论
     *
     * @param id
     * @return int
     * 创建人:yykk
     * 创建时间：2022-04-18 01:07:39
     * @version 1.0.0
     */
    @Override
    public int deleteHotelCommentById(Long id) {
        // 根据父id查询下面头没有子评论
        HotelComment hotelComment = this.getHotelCommentById(id);
        if (Vsserts.isNotNull(hotelComment)) {
            Long parentId = hotelComment.getParentId();
            if (Vsserts.isNotNull(parentId) && parentId.equals(0L)) {
                // 代表是父评论，查询父评论下有没有子评论
                LambdaQueryWrapper<HotelComment> lambdaQueryWrapper = new LambdaQueryWrapper<>();
                lambdaQueryWrapper.eq(HotelComment::getParentId, id);
                List<HotelComment> commentList = this.list(lambdaQueryWrapper);
                List<Long> ids = commentList.stream().map(com -> com.getId()).collect(Collectors.toList());
                if (CollectionUtils.isNotEmpty(ids)) {
                    boolean b = this.removeByIds(ids);
                    return b ? 1 : 0;
                }
            } else {
                boolean b = this.removeById(id);
                return b ? 1 : 0;
            }
        }
        return 0;
    }

    /**
     * 根据id删除
     *
     * @param ids
     * @return boolean
     * 创建人:yykk
     * 创建时间：2022-04-18 01:07:39
     * @version 1.0.0
     */
    @Override
    public boolean delBatchHotelComment(String ids) {
        try {
            // 1 : 把数据分割
            String[] strings = ids.split(",");
            // 2: 组装成一个List<HotelComment>
            List<HotelComment> hotelcommentList = Arrays.stream(strings).map(idstr -> {
                HotelComment hotelcomment = new HotelComment();
                hotelcomment.setId(new Long(idstr));
                return hotelcomment;
            }).collect(Collectors.toList());
            // 3: 批量删除
            List<Long> idList = hotelcommentList.stream().map(com -> com.getId()).collect(Collectors.toList());
            if (CollectionUtils.isNotEmpty(idList)) {
                return this.removeByIds(idList);
            }
        } catch (Exception ex) {
            throw new PugValidationException(ResultStatusEnumA.SERVER_DB_ERROR);
        }
        return false;
    }


}